package com.qianguo.sender.tab;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

import com.qianguo.sender.R;
import com.qianguo.sender.base.BaseFragment;
import com.qianguo.sender.order.DoingOrderFragment_;
import com.qianguo.sender.order.DoneOrderFragment_;

import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

/**
 * 订单tab
 *
 * @author eva
 * @date 16/4/29
 */
@EFragment(R.layout.fragment_sender_order)
public class SenderOrderFragment extends BaseFragment {

    private static final int STATUS_DOING = 0;
    private static final int STATUS_DONE = 1;

    private static String[] ORDER_STATUSES;

    @ViewById(R.id.sender_catch_order_tab_layout)
    TabLayout mTabLayout;
    @ViewById(R.id.sender_catch_order_view_pager)
    ViewPager mViewPager;

    private OrderFragmentAdapter mAdapter;
    @Override
    protected void initializeUI() {
        ORDER_STATUSES = getResources().getStringArray(R.array.order_statuses);

        mAdapter = new OrderFragmentAdapter(getChildFragmentManager());
        mViewPager.setAdapter(mAdapter);
        mTabLayout.setTabMode(TabLayout.MODE_FIXED);
        mTabLayout.post(new Runnable() {
            @Override
            public void run() {
                mTabLayout.setupWithViewPager(mViewPager);
            }
        });
    }

    public class OrderFragmentAdapter extends FragmentPagerAdapter {

        public OrderFragmentAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            Fragment f = null;
            switch (position) {
                case STATUS_DOING:
                    f = new DoingOrderFragment_();
                    break;
                case STATUS_DONE:
                    f = new DoneOrderFragment_();
                    break;
            }
            return f;
        }

        @Override
        public int getCount() {
            return ORDER_STATUSES.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return ORDER_STATUSES[position];
        }
    }
}
